package com.gxan.dao.impl;

import com.gxan.dao.RecruitmentDao;
import com.gxan.dbutils.JdbcBuild;
import com.gxan.pojo.Recruitment;

import java.sql.SQLException;
import java.util.List;

public class RecruitmentDaoImpl extends JdbcBuild implements RecruitmentDao {

    @Override
    public int addRecruitment(Recruitment recruitment) throws SQLException {
        //定义Sql语句
        String sql = "insert  into `recruitment`(`dept`,`recruitment_type`,`recruiting_numbers`,`job_title`,`job_code`,`job_classification`,`registrant`,`record_date`,`expiration_date`,`job_description`,`recruitment_description`) values (?,?,?,?,?,?,?,?,?,?,?);";
        Object params[]={recruitment.getDept(),recruitment.getRecruitment_type(),recruitment.getRecruiting_numbers(),recruitment.getJob_title(),recruitment.getJob_code(),recruitment.getJob_classification(),recruitment.getRegistrant(),recruitment.getRecord_date(),recruitment.getExpiration_date(),recruitment.getJob_description(),recruitment.getRecruitment_description()};
        return super.executeUpdate(sql,params);
    }

    @Override
    public int delRecruitmentById(String id) throws SQLException {
        String sql="delete from recruitment  where staff_id = ?";
        Object[] params = {id};
        return super.executeUpdate(sql,params);
    }

    @Override
    public int updateRecruitment(Recruitment recruitment) throws SQLException {
        //定义Sql语句
        String sql = "UPDATE recruitment SET dept = ?,recruitment_type= ?,recruiting_numbers= ?,job_title= ?,job_code= ?,job_classification= ?,registrant= ?,record_date= ?,expiration_date= ?,job_description= ?,recruitment_description= ? where staff_id = ?";
        Object params[]={recruitment.getDept(),recruitment.getRecruitment_type(),recruitment.getRecruiting_numbers(),recruitment.getJob_title(),recruitment.getJob_code(),recruitment.getJob_classification(),recruitment.getRegistrant(),recruitment.getRecord_date(),recruitment.getExpiration_date(),recruitment.getJob_description(),recruitment.getRecruitment_description(),recruitment.getStaff_id()};
        return super.executeUpdate(sql,params);
    }

    @Override
    public Integer countRecruitment(Recruitment recruitment) throws SQLException {
        if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()==null &&recruitment.getRecord_date()==null &&recruitment.getExpiration_date()==null){
            //只按照职位名称查询
            String sql="select count(*) from recruitment where job_title = ? ";
            Object params[]={recruitment.getJob_title()};
            return super.findAllForInt(sql,params);
        }else if(recruitment.getJob_title()==null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()==null &&recruitment.getExpiration_date()==null){
            //只按照招聘分类查询
            String sql="select count(*) from recruitment where recruitment_type = ? ";
            Object params[]={recruitment.getRecruitment_type()};
            return super.findAllForInt(sql,params);
        }else if(recruitment.getJob_title()==null && recruitment.getRecruitment_type()==null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //只按照日期查询
            String sql="select count(*) from recruitment where record_date >= ? and expiration_date <= ?";
            Object params[]={recruitment.getRecord_date(),recruitment.getExpiration_date()};
            return super.findAllForInt(sql,params);
        }else if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()==null &&recruitment.getExpiration_date()==null){
            //按照职位名称查询and按照招聘分类查询
            String sql="select count(*) from recruitment where job_title = ?  and recruitment_type = ?";
            Object params[]={recruitment.getJob_title(),recruitment.getRecruitment_type()};
            return super.findAllForInt(sql,params);
        }else if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()==null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //按照职位名称查询and按照日期查询
            String sql="select count(*) from recruitment where job_title = ?  and record_date >= ? and expiration_date <= ?";
            Object params[]={recruitment.getJob_title(),recruitment.getRecord_date(),recruitment.getExpiration_date()};
            return super.findAllForInt(sql,params);
        }else if(recruitment.getJob_title()==null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //按照招聘分类查询and按照日期查询
            String sql="select count(*) from recruitment where recruitment_type = ? and record_date >= ? and expiration_date <= ?";
            Object params[]={recruitment.getRecruitment_type(),recruitment.getRecord_date(),recruitment.getExpiration_date()};
            return super.findAllForInt(sql,params);
        }else if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //按照职位名称查询and按照招聘分类查询and按照日期查询
            String sql="select count(*) from recruitment where job_title = ? and recruitment_type = ? and record_date >= ? and expiration_date <= ?";
            Object params[]={recruitment.getJob_title(),recruitment.getRecruitment_type(),recruitment.getRecord_date(),recruitment.getExpiration_date()};
            return super.findAllForInt(sql,params);
        }else{
            //无条件查询
            String sql="select count(*) from recruitment ";
            return super.findAllForInt(sql,null);
        }

    }

    @Override
    public List<Recruitment> queryRecruitmentList(Integer param01, Integer param02, Recruitment recruitment) throws SQLException {
        if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()==null &&recruitment.getRecord_date()==null &&recruitment.getExpiration_date()==null){
            //只按照职位名称查询
            String sql="select * from recruitment where job_title = ? limit ? , ? ";
            Object params[]={recruitment.getJob_title(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else if(recruitment.getJob_title()==null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()==null &&recruitment.getExpiration_date()==null){
            //只按照招聘分类查询
            String sql="select * from recruitment where recruitment_type = ? limit ?,? ";
            Object params[]={recruitment.getRecruitment_type(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else if(recruitment.getJob_title()==null && recruitment.getRecruitment_type()==null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //只按照日期查询
            String sql="select * from recruitment where record_date >= ? and expiration_date <= ? limit ? , ? ";
            Object params[]={recruitment.getRecord_date(),recruitment.getExpiration_date(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()==null &&recruitment.getExpiration_date()==null){
            //按照职位名称查询and按照招聘分类查询
            String sql="select * from recruitment where job_title = ?  and recruitment_type = ? limit ? , ? ";
            Object params[]={recruitment.getJob_title(),recruitment.getRecruitment_type(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()==null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //按照职位名称查询and按照日期查询
            String sql="select * from recruitment where job_title = ?  and record_date >= ? and expiration_date <= ? limit ? , ? ";
            Object params[]={recruitment.getJob_title(),recruitment.getRecord_date(),recruitment.getExpiration_date(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else if(recruitment.getJob_title()==null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //按照招聘分类查询and按照日期查询
            String sql="select * from recruitment where recruitment_type = ? and record_date >= ? and expiration_date <= ? limit ? , ? ";
            Object params[]={recruitment.getRecruitment_type(),recruitment.getRecord_date(),recruitment.getExpiration_date(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else if(recruitment.getJob_title()!=null && recruitment.getRecruitment_type()!=null &&recruitment.getRecord_date()!=null &&recruitment.getExpiration_date()!=null){
            //按照职位名称查询and按照招聘分类查询and按照日期查询
            String sql="select * from recruitment where job_title = ? and recruitment_type = ? and record_date >= ? and expiration_date <= ? limit ? , ? ";
            Object params[]={recruitment.getJob_title(),recruitment.getRecruitment_type(),recruitment.getRecord_date(),recruitment.getExpiration_date(),param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }else{
            //无条件查询
            String sql="select * from recruitment limit ? , ?  ";
            Object params[]={param01,param02};
            return super.queryListEntity(Recruitment.class,sql,params);
        }
    }


}
